import API_ENDPOINTS from '@/lib/apiEndpoints';
import { apiRequest } from '@gm/base/server';
import useSWR from 'swr';
import z from 'zod';

const getCaptchaSchema = z.object({
  captchaType: z.enum(['1', '2']),
});
type GetCaptcha = z.infer<typeof getCaptchaSchema>;

export function getCaptcha(body: GetCaptcha) {
  const { error: err, success } = getCaptchaSchema.safeParse(body);
  if (!success) {
    throw err;
  }
  const { data, error, isLoading } = useSWR(API_ENDPOINTS.CAPTCHA.POST, (url) =>
    apiRequest<CaptchaResults>(url, {
      method: 'POST',
      body,
    }),
  );

  return {
    data,
    isLoading,
    isError: error,
  };
}
